package com.mars.algorithm.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * @Author King
 * @create 2020/8/13 15:09
 */
public class KingLock {

    public static void main(String[] args) {

        semaphore();
        //countDownLatch();
        //cyclicBarrier();
    }

    private static void semaphore() {

        final Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() + "等待获取到许可");
                        semaphore.acquire();
                        System.out.println(Thread.currentThread().getName() + "获取到许可");
                        Thread.sleep(2000);
                        semaphore.release();
                        System.out.println(Thread.currentThread().getName() + "执行完释放许可");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    private static void countDownLatch() {

        final CountDownLatch countDownLatch = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName() + "我要上车中....");
                        Thread.sleep((long) (Math.random() * 10000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                        System.out.println(Thread.currentThread().getName() + "我上车了，还剩:" + countDownLatch.getCount() + "个人没上车");
                    }
                }
            }).start();
        }


        try {
            System.out.println(Thread.currentThread().getName() + "等待所有人上车，我就发车");
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName() + "所有人上车了，发车了");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void cyclicBarrier() {

        final CyclicBarrier cyclicBarrier = new CyclicBarrier(5);

        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() + "进入游戏中。。。");
                        Thread.sleep((long) (Math.random() * 10000));
                        System.out.println(Thread.currentThread().getName() + "我加载100%了");
                        cyclicBarrier.await();
                        System.out.println(Thread.currentThread().getName() + "全都加载好了，游戏开始了");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }








}
